import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "login" */ '../components/Login.vue')
  },
  {
    path: '/home',
    component: () => import(/* webpackChunkName: "home" */ '../components/Home.vue'),
    redirect: '/Stree',
    children: [
      {
        path: '/welcome',
        show: true,
        component: () => import(/* webpackChunkName: "welcome" */ '../components/WelCome.vue')
      },
      {
        path: '/mine',
        show: true,
        component: () => import(/* webpackChunkName: "mine" */ '../components/my/mine.vue')
      },
      {
        path: '/works',
        component: () => import(/* webpackChunkName: "works" */ '../components/Work/works.vue')
      },
      {
        path: '/residentList',
        show: true,
        component: () => import(/* webpackChunkName: "residentList" */ '../components/Resident/residentList.vue')
      },
      {
        path: '/addOrder',
        component: () => import(/* webpackChunkName: "addOrder" */ '../components/Order/addOrder.vue')
      },
      {
        path: '/myOrder',
        component: () => import(/* webpackChunkName: "myOrder" */ '../components/Order/myOrder.vue')
      },
      {
        path: '/mywork',
        component: () => import(/* webpackChunkName: "mywork" */ '../components/Work/mywork.vue')
      },
      {
        path: '/allwork',
        component: () => import(/* webpackChunkName: "allWork" */ '../components/Work/allWork.vue')
      },
      {
        path: '/goods',
        component: () => import(/* webpackChunkName: "allWork" */ '../components/Epidemic/goods.vue')
      },
      {
        path: '/disinfect',
        component: () => import(/* webpackChunkName: "disinfect" */ '../components/Epidemic/disinfect.vue')
      },
      {
        path: '/changpassword',
        component: () => import(/* webpackChunkName: "changpassword" */ '../components/my/changePassword.vue')
      },
      {
        path: '/myCharts',
        component: () => import(/* webpackChunkName: "changpassword" */ '../components/Information/myCharts.vue')
      },
      {
        path: '/Stree',
        component: () => import(/* webpackChunkName: "changpassword" */ '../components/Information/Stree.vue')
      },
      {
        path: '/allLogs',
        component: () => import(/* webpackChunkName: "changpassword" */ '../components/logs/allLogs.vue')
      },
      {
        path: '/myLogs',
        component: () => import(/* webpackChunkName: "changpassword" */ '../components/logs/myLogs.vue')
      },
      {
        path: '/house',
        component: () => import(/* webpackChunkName: "changpassword" */ '../components/Resident/house.vue')
      }
    ]
  },
  {
    path: '/',
    redirect: '/login'
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 定义游客可以访问的路径数组集合(相当于白名单)
  const noLoginPath = ['/home', '/home/detail']
  if (noLoginPath.includes(to.path)) {
    next()
  }

  if (to.path) { if (to.path === '/login') { return next() } }

  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
